import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import rootReducer from 'reducers';
import { createStore } from 'redux';
import routes from '../common/routes';
import './index.less';
import axios from 'axios';

const store = createStore(rootReducer)


axios.interceptors.request.use(function (config) {
  const { headers } = config;
  const { token } = localStorage;
  headers.token = token;
  headers.Authorization = token;
  return config;
}, function (error) {
  return Promise.reject(error);
});

axios.interceptors.response.use(function (response) {
  const { data } = response;

  if (data.code === 403) {
    localStorage.removeItem('token');
    location.href = '../common/pages/login';
  }

  return response;
}, function (error) {
  return Promise.reject(error);
});
ReactDOM.render(
  <Provider store={store}>
    { routes }
  </Provider>,
  document.getElementById('root')
);